package com.xnx3.wangmarket.plugin.ainewspublish.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.SpringUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.entity.News;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.util.SiteUtil;
import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;

/**
 * 网站管理后台-功能插件 点开的页面
 * @author 管雷鸣
 */
@Controller(value="AiNewsPublishSiteAdminPluginNewsController")
@RequestMapping("/plugin/ainewspublish/")
public class SiteAdminController extends BasePluginController {
	@Resource
	private SqlService sqlService;
	
	@RequestMapping(value="index.do")
	public String index(HttpServletRequest request, Model model){
		
		Site site = getSite();
		if(site == null) {
			return error(model, "尚未登录");
		}
		User user = getUser();
		
		//检测是否有符合条件的栏目，也就是 是信息列表类型的栏目
		List<SiteColumn> columnList = sqlService.findBySqlQuery("SELECT * FROM site_column WHERE siteid = "+site.getId(), SiteColumn.class);
		//找出是信息列表的栏目
		List<SiteColumn> xinxiColumnList = new ArrayList<SiteColumn>();
		for (int i = 0; i < columnList.size(); i++) {
			SiteColumn column = columnList.get(i);
			if(column.getType() - SiteColumn.TYPE_LIST == 0) {
				xinxiColumnList.add(column);
			}
		}
		if(xinxiColumnList.size() == 0) {
			return error(model, "您当前网站并没有 信息列表 类型的栏目！只有这种类型的栏目才能发布文章的，也就才能使用AI来自动发文章");
		}
		
		//找到第一个不是父栏目的code，也就是这个栏目是能发布文章的
		SiteColumn okColumn = null; 
		for (int i = 0; i < xinxiColumnList.size(); i++) {
			SiteColumn column = xinxiColumnList.get(i);
			if(!SiteUtil.isParentColumn(columnList, column)) {
				//不是夫栏目，才符合要求
				okColumn = column;
				break;
			}
		}
		if(okColumn == null) {
			return error(model, "您当前网站并没有可发布文章栏目是 信息列表 类型的栏目！因为我们自检发现你的这个类型的栏目是父栏目，它下面还有子栏目，子栏目（也就是最下级栏目）必须是这个类型的才能发布文章");
		}
		
		model.addAttribute("password", site.getId()+"_"+user.getPassword());
		model.addAttribute("site", site);
		model.addAttribute("column", okColumn);
		return "plugin/ainewspublish/index";
	}
}